package cn.tedu.boot3_1.Controller;

import cn.tedu.boot3_1.enyity.Emp;
import cn.tedu.boot3_1.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

@RestController
public class EmpController {
    @RequestMapping("/add")
    public String add(Emp emp){
        System.out.println("emp = " + emp);
        try (Connection conn= DBUtils.getConn();){
            //准备插入数据的SQL语句
            String sql="insert into newemp value(null,?,?,?)";
            //因为有变量 创建预编译的对象执行SQL
            PreparedStatement ps=conn.prepareStatement(sql);
            //替换
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getSal());
            //执行语句
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加完成!<a href='javascript:history.go(-2)'>回到首页</a>";
    }
    @RequestMapping("/select")
    public String select(){
        //创建一个集合来装查询到的员工对象
        ArrayList<Emp>list=new ArrayList<>();
        try (Connection conn=DBUtils.getConn();){
            String sql="select *from newemp";
            //没有变量 直接Statement
            Statement s=conn.createStatement();
            //执行查询结果
            ResultSet rs=s.executeQuery(sql);
            //遍历查询结果
            while (rs.next()){
                int id=rs.getInt(1);
                String name=rs.getString(2);
                String job=rs.getString(3);
                int sal=rs.getInt(4);
                //把查询到的数据封装到emp对象中
                Emp emp=new Emp();
                emp.setId(id);
                emp.setName(name);
                emp.setJob(job);
                emp.setSal(sal);
                //把对象装进集合
                list.add(emp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        String html="<table border=1>";
        html+="<caption>员工列表M</caption>";
        html+="<tr><th>id</th><th>名字</th><th>工作</th><th>工资</th><th>操作</th></tr>";
        //遍历员工对象
        for(Emp e:list){
            html+="<tr>";
            html+="<td>"+e.getId()+"</td>";
            html+="<td>"+e.getName()+"</td>";
            html+="<td>"+e.getJob()+"</td>";
            html+="<td>"+e.getSal()+"</td>";
            html+="<td><a href='/delete?id="+e.getId()+"'>删除</td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
    //添加处理请求的方法
    @RequestMapping("/delete")
    public void delete(int id ,HttpServletResponse response) throws IOException {
        System.out.println("id = " + id);
        try (Connection conn=DBUtils.getConn();){
            String sql="delete from newemp where id=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            //替换?
            ps.setInt(1,id);
            //执行
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //重定向
        //执行流程: 服务器会向客户段浏览器返回一个302状态码和一个地址
        //浏览器接收到302后会立即向指定的地址再次发出请求
        response.sendRedirect("/select");
    }
    @RequestMapping("/update")
    public void update(Emp emp,HttpServletResponse response) throws IOException {
        try (Connection conn=DBUtils.getConn()){
            String sql="update newemp set name=?,job=?,sal=? where id=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getSal());
            ps.setInt(4,emp.getId());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        response.sendRedirect("/select");
    }
}







